import { _decorator, Component, CCInteger, Collider2D, Contact2DType } from 'cc'
const { ccclass, property } = _decorator

@ccclass('Bullet')
export class Bullet extends Component {
  @property({ type: CCInteger })
  speed: number = 400

  private _collider: Collider2D = null
  start() {
    this._collider = this.node.getComponent(Collider2D)
    if (this._collider) {
      // this._collider.on(Contact2DType.BEGIN_CONTACT, this.onBegainContact, this)
    }
  }

  update(deltaTime: number) {
    const { position } = this.node
    this.node.setPosition(position.x, position.y + this.speed * deltaTime, position.z)
    if (position.y > 430) {
      this.node.destroy()
    }
  }
  onBegainContact() {
    if (this.node) {
      this.node.destroy()
    }
  }
  protected onDestroy(): void {
    if (this._collider) {
      this._collider.off(Contact2DType.BEGIN_CONTACT, this.onBegainContact, this)
    }
  }
}
